package cn.ming.leo.resource;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cn.ming.leo.bean.Door;
import cn.ming.leo.bean.DoorTools;
import cn.ming.leo.bean.Tools;
import cn.ming.leo.bean.User;
import cn.ming.leo.service.ToolsService;
import cn.ming.leo.utils.Constant.MsgCode;
import cn.ming.leo.utils.Message;
import cn.ming.leo.utils.PageListModel;
import cn.ming.leo.utils.TimeFormat;

import com.avaje.ebean.Ebean;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.PagedList;
import com.avaje.ebean.SqlRow;

@RestController
@RequestMapping("/tools")
public class ToolsResource {

	@Autowired
	private ToolsService toolsService;

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public Message<String> insert(@RequestBody Tools tools) {
		return toolsService.insert(tools.getId(), tools.getRoles(),
				tools.getLogoUrl(), tools.getName(), tools.getSpec(),
				tools.getBorrowDay(), tools.getDoorId());
	}

	@RequestMapping(value = "/cancel", method = RequestMethod.POST)
	@ResponseBody
	public Message<String> cancel(Integer id) {
		Tools tools = Tools.find.byId("" + id);
		tools.setToolsStatus(0);
		tools.update();

		String sql1 = "select id from door_tools where tools_id = " + id
				+ " order by create_time desc limit 1";
		SqlRow row = Ebean.createSqlQuery(sql1).findUnique();

		DoorTools doorTools = DoorTools.find.byId(row.getString("id"));
		doorTools.setStatus(7);
		doorTools.update();

		return new Message<String>("success");
	}

	@RequestMapping(value = "/findAll", method = RequestMethod.GET)
	public Message<PageListModel<Tools>> findAll1(Integer userId,
			Integer pageIndex, Integer pageSize) {
		return toolsService.findAll(userId, pageIndex, pageSize);
	}

	/**
	 * 预约
	 * 
	 * @author 01381167
	 * @since 2018年12月24日
	 * @version V1.5
	 */
	@RequestMapping(value = "/makeTools", method = RequestMethod.POST)
	public Message<String> updateStatus(int id, int userId) {
		Tools tools = Tools.find.byId("" + id);

		if (tools.getToolsStatus() == 0 || tools.getToolsStatus() == 4
				|| tools.getToolsStatus() == 5) {
			tools.setToolsStatus(1);
			tools.update();

			Door door = Door.find.where().eq("tools_id", id).findUnique();

			DoorTools doorTools = new DoorTools();
			doorTools.setCreateTime(TimeFormat.nowTime());
			doorTools.setDoorId(door.getId());
			doorTools.setToolsId(id);
			doorTools.setUpdateTime(new Date());
			doorTools.setPutUserId(userId);
			doorTools.setStatus(1);
			doorTools.insert();

		} else {
			return new Message<String>(MsgCode.FAIL.value, "工具暂不能被预约");
		}
		return new Message<String>("success");
	}

	@RequestMapping(value = "/findDoorTools", method = RequestMethod.GET)
	public Message<PageListModel<DoorTools>> findAll(Integer userId,
			Integer status, Integer pageIndex, Integer pageSize) {
		PagedList<DoorTools> pl;
		ExpressionList<DoorTools> pression = Ebean.find(DoorTools.class)
				.where();

		pageIndex = (pageIndex == null) ? 0 : pageIndex - 1;
		pageSize = (pageSize == null) ? 10 : pageSize;

		if (status != null) {
			pression = pression.eq("put_user_id", userId).eq("status", status);
		} else {
			pression = pression.eq("put_user_id", userId);
		}

		pl = pression.order().desc("create_time")
				.findPagedList(pageIndex, pageSize);

		PageListModel<DoorTools> pageListModel = new PageListModel<DoorTools>(
				pl);

		for (DoorTools doorTools : pageListModel.getList()) {
			doorTools.setPutUserName(User.find.byId(
					"" + doorTools.getPutUserId()).getMobile());

			if (doorTools.getTakeUserId() != 0) {
				doorTools.setTakeUserName(User.find.byId(
						"" + doorTools.getTakeUserId()).getMobile());
			}

			Door door = Door.find.where()
					.eq("tools_id", doorTools.getTools().getId()).findUnique();
			doorTools.getTools().setDoorName(
					door.getBox().getBoxNo() + "柜子编号" + door.getDoorNo()
							+ "号格子");

		}
		return new Message<PageListModel<DoorTools>>(pageListModel);
	}
}
